<template>
  <div class="app_content">
      <el-form :inline="true" ref="queryForm" :model="queryParams" class="demo-form-inline" :size="size" v-if="showSearch">
        <el-form-item label="系统模块" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入系统模块"
          clearable
          style="width: 240px;"
        />
      </el-form-item>
      <el-form-item label="操作人员" prop="operName">
        <el-input
          v-model="queryParams.operName"
          placeholder="请输入操作人员"
          clearable
          style="width: 240px;"
        />
      </el-form-item>
      <el-form-item label="类型" prop="businessType" label-width="70px">
        <el-select
          v-model="queryParams.businessType"
          placeholder="操作类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in operType"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status" label-width="70px">
        <el-select
          v-model="queryParams.status"
          placeholder="登录状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="querySubmit">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-search'"/>
          搜索</el-button>
        <el-button   @click="resetSubmit">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-redo'"/>
          重置</el-button>
      </el-form-item>
      </el-form>
       <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            :size="size"
            :disabled="multiple"
            @click="handleDelete"
            v-hasRole="['system:operlog:remove']"
          ><component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-delete-five'"/>删除</el-button>
        </el-col>
        <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          :size="size"
          @click="handleClean"
          v-hasRole="['system:operlog:remove']"
        ><component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-clear'"/>清空</el-button>
      </el-col>
        <!-- <el-col :span="1.5">
          <Export  :dc="true"  :dr="true" :exlData="tableData" :tHead="staticData.ExportHead" :tName="'导出的标题'" ></Export>
        </el-col>
  -->
        <!-- <RightToolbar :showSearch="showSearch" @queryTable="getList" :columns="columns"/> -->
        <RightToolbar :showSearch="showSearch" @queryTable='getList' @showSearchClick='showSearchClick' :columns="columns"/>
      </el-row>
      <el-table :border="$border" v-loading="loading" :data="tableData" :height="maxHeight"  style="width: 100%" @selection-change="handleSelectionChange"  :size="size"
      >
      <el-table-column type="selection" width="55" />
      <el-table-column label="编号" prop="id" show-overflow-tooltip key="id" v-if="columns[0].visible" width="60px">
        <template #default="scope">{{scope.$index+1}}</template>
        </el-table-column>
      <el-table-column label="系统模块" align="center" prop="title" :show-overflow-tooltip="true"  key="title" v-if="columns[1].visible">
        <template #default="scope">
          <span @click="handleView(scope.row)" class="span_a"> {{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作类型" align="center" :formatter="typeFormat" s prop="businessType" width="130" :show-overflow-tooltip="true" key="businessType" v-if="columns[2].visible"/>
      <el-table-column label="请求方式" align="center" prop="requestMethod" :show-overflow-tooltip="true"  key="requestMethod" v-if="columns[3].visible"/>
      <el-table-column label="操作人员" align="center" prop="operName" :show-overflow-tooltip="true"  key="operName" v-if="columns[4].visible"/>
      <el-table-column label="操作地址" align="center" prop="operIp" show-overflow-tooltip key="operIp" v-if="columns[5].visible"/>
      <el-table-column label="操作地点" align="center" prop="operLocation" show-overflow-tooltip  key="operLocation" v-if="columns[6].visible"/>
      <el-table-column label="操作状态"  :formatter="statusFormat" show-overflow-tooltip  align="center" prop="status" key="status" v-if="columns[7].visible">
      </el-table-column>
      <el-table-column label="操作时间" prop="operTime" show-overflow-tooltip key="operTime" v-if="columns[8].visible"/>
      <!-- <el-table-column label="操作" fixed="right" align="center"  width="250px">
      <template #default="scope">
          <el-button :size="size" type="success" :icon="View" text @click="handleView(scope.row)"
          >详情</el-button
          >
      </template>
      </el-table-column> -->
    </el-table>
  <div style="margin-top:10px;position: absolute;right: 0;margin-right:20px">
      <el-pagination
        v-model:currentPage="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
  </div>

  <!-- <el-dialog  v-model="open"  :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" width="600px" append-to-body draggable :fullscreen="fullscreen">
      <template #header="{close,titleId, titleClass }">
      <div class="my-header">
        <span :id="titleId" :class="titleClass">{{title}}</span>
        <div>
          <component
        style="cursor:pointer;line-height: 24px;margin-right:10px;"
        v-if="fullscreen==false"
              @click="fullscreen=true"
              class="menu-icon"
              theme="outline"
              size="14"
              strokeWidth="3"
              :is="'icon-full-screen-one'"
            />
            <component
            v-else
        style="cursor:pointer;line-height: 24px;margin-right:10px;"
              @click="fullscreen=false"
              class="menu-icon"
              theme="outline"
              size="14"
              strokeWidth="3"
              :is="'icon-off-screen-one'"
            />
        <component
        style="cursor:pointer;line-height: 24px;"
              @click="close"
              class="menu-icon"
              theme="outline"
              size="14"
              strokeWidth="3"
              :is="'icon-close'"
            />
        </div>
        
      </div>
    </template>
    <el-form ref="formDataRef" :model="formData" label-width="100px" :size="size">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：">{{ formData.title }} / {{ typeFormat(formData) }}</el-form-item>
            <el-form-item
              label="登录信息："
            >{{ formData.operName }} / {{ formData.operIp }} / {{ formData.operLocation }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求地址：">{{ formData.operUrl }}</el-form-item>
            <el-form-item label="请求方式：">{{ formData.requestMethod }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作方法：">{{ formData.method }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数：">{{ formData.operParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数：">{{ formData.jsonResult }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作状态：">
              <div v-if="formData.status === 0">正常</div>
              <div v-else-if="formData.status === 1">失败</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间：">{{formData.operTime }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="异常信息：" v-if="formData.status === 1">{{ formData.errorMsg }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>

    </el-dialog> -->
  
    <el-drawer
      :title="title"
      v-model="open"
      direction="rtl"
      destroy-on-close
      size="50%"
      :close-on-click-modal="false"
    >
    <el-form ref="formDataRef" :model="formData" label-width="100px" :size="size">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：">{{ formData.title }} / {{ typeFormat(formData) }}</el-form-item>
            <el-form-item
              label="登录信息："
            >{{ formData.operName }} / {{ formData.operIp }} / {{ formData.operLocation }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求地址：">{{ formData.operUrl }}</el-form-item>
            <el-form-item label="请求方式：">{{ formData.requestMethod }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作方法：">{{ formData.method }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数：">{{ formData.operParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数：">{{ formData.jsonResult }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作状态：">
              <div v-if="formData.status === 0">正常</div>
              <div v-else-if="formData.status === 1">失败</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间：">{{formData.operTime }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="异常信息：" v-if="formData.status === 1">{{ formData.errorMsg }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
  </el-drawer>
  </div>
  
</template>

<script  >
import { list, delOperlog, cleanOperlog } from "@/api/monitor/operlog";
import { computed, ref ,reactive,onMounted,getCurrentInstance,toRefs} from 'vue'
import { useStore } from 'vuex';
import { useI18n } from 'vue-i18n';
export default defineComponent({
setup(){
  const { proxy } = getCurrentInstance();//关键代码
  const queryForm=ref(null)
  const { t } = useI18n();
  const store = useStore();
  const staticData=reactive({
    formData:{
    },
    maxHeight:store.getters['setting/mode']=='horizontal'?document.body.clientHeight - 320:store.getters['setting/isFullScreen']!=false ?document.body.clientHeight - 360:document.body.clientHeight - 370,
    ids:[],
    titleName:[],
    single:true,
    multiple:true,
    showSearch:true,
    background:false,
    disabled :false,
    search:'',
    tableData:[],
    // 日期范围
    dateRange: [],
    // 菜单状态数据字典
    statusOptions: [],
    operType:[],
    //是否全屏
    fullscreen:false,
    // 是否显示弹出层
    open: false,
    // 弹出层标题
    title: "",
    // 查询参数
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      title: undefined,
      operName: undefined,
      businessType: undefined,
      status: undefined
    },
    total:0,
    loading:true, 
    ExportHead:{
      id: "编号",
    },
    // 列信息
    columns: [
      { key: 0, label: `编号`, visible: true },
      { key: 1, label: `用户名称`, visible: true },
      { key: 2, label: `登录地址`, visible: true },
      { key: 3, label: `登录地点`, visible: true },
      { key: 4, label: `浏览器`, visible: true },
      { key: 5, label: `操作系统`, visible: true },
      { key: 6, label: `登录状态`, visible: true },
      { key: 7, label: `操作信息`, visible: true },
      { key: 8, label: `登录时间`, visible: true },
      
    ],
  })
  const size = computed(() => {
        return store.getters['setting/size'];
  });

  const small =computed(() => {
    return size.value=='small'?true:false
  });

    onMounted(() => {
      proxy.$getDicts("sys_common_status").then(response => {
        staticData.statusOptions = response.data;
      });
      proxy.$getDicts("sys_oper_type").then(response => {
        staticData.operType = response.data;
      });
      proxy.getList()
      })

  const methods = {
     getList(){
        staticData.loading = true;
        list(staticData.queryParams).then(
          response => {
            staticData.tableData = response.rows;
            staticData.total = response.total;
            staticData.loading = false;
          }
      );
     },
    // 字典翻译
    statusFormat(row, column) {
      const data=proxy.$selectDictLabel(staticData.statusOptions, row.status)
      let stateClass
      // if(data=='正常'){
      //   stateClass='status_success'
      // }else{
      //   stateClass='status_error'
      // }
      
      return data;
    },
    // 字典翻译
    typeFormat(row, column) {
      const data=proxy.$selectDictLabel(staticData.operType, row.businessType)
      let stateClass
      // if(data=='正常'){
      //   stateClass='status_success'
      // }else{
      //   stateClass='status_error'
      // }
      
      return data;
    },

     /**
      * 显示或隐藏搜索条件
      **/
     showSearchClick(data){
       staticData.showSearch=data
     },
    
    // 多选框选中数据
    handleSelectionChange(selection) {
      staticData.ids = selection.map(item => item.operId);
      staticData.titleName = selection.map(item => item.title);
      staticData.single = selection.length != 1;
      staticData.multiple = !selection.length;
    },
    /** 详细按钮操作 */
    handleView(row) {
      staticData.formData = row;
      staticData.fullscreen=false
      staticData.title = "详情";
      staticData.open = true;
      
    },
    /** 清空按钮操作 */
    handleClean() {
      ElMessageBox.confirm('是否确认清空所有登录日志数据项？', t('confirm.title'), {
          confirmButtonText: t('btn.confirm'),
          cancelButtonText: t('btn.cancel'),
          type: "warning"
        })
      .then(function() {
        return cleanOperlog();
      }).then(() => {
        proxy.getList();
        proxy.Success("清空成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.infoId  || staticData.ids;
      const titleName = row.title || staticData.titleName;
      
      ElMessageBox.confirm('是否确认删除日志用户为"' + titleName + '"的数据项？', t('confirm.title'), {
          confirmButtonText: t('btn.confirm'),
          cancelButtonText: t('btn.cancel'),
          type: "warning"
        })
      .then(function() {
        return delOperlog(infoIds);
      }).then(() => {
        proxy.getList();
        proxy.Success("删除成功");
      }).catch(() => {});
    },
    resetSubmit(){
      staticData.dateRange=[]
      queryForm.value.resetFields();
      proxy.getList()
    },
    querySubmit(){
      staticData.queryParams.pageNum = 1;
      proxy.getList()
    },
    handleSizeChange (val) {
      console.log(`${val} items per page`)
      staticData.queryParams.pageSize =val;
      proxy.getList()
    },
    handleCurrentChange(val){
      console.log(`current page: ${val}`)
      staticData.queryParams.pageNum =val;
      proxy.getList()
    }
   }
    return {
      size,
      small,
      queryForm,
      ...methods,
      ...toRefs(staticData)
    }
}


})



</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
margin-top: 10px;
}
.demo-pagination-block .demonstration {
margin-bottom: 16px;
}
</style>
<style scoped>
.app_content{
  
  margin-top: 30px;
  margin-left: 10px;
}
.mb8{
  margin-bottom: 10px;
}
</style>